// pages/editInfo/editInfo.js
const HTTP = require('../../utils/util');
const Common = require('../../libs/common');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        showId: true,
        next: false,
        phone: '',
        verifyCode: '',
        new_pass_word:'',
        old_pass_word:''
    },

    handleVerifyCode(e) {
        this.setData({
            verifyCode: e.detail.value
        });
    },
    handlePhone(e) {
        this.setData({
            phone: e.detail.value
        });
    },
    postPhone() {
        HTTP.postData('/api/person/modifyMobile', {
            verify_code: Number(this.data.verifyCode),
            mobile: Number(this.data.phone)
        }, res => {
            // console.log(res);
            wx.navigateBack();
        });
    },
    doNext(e) {
        if(this.data.old_pass_word.length !== 0){
            this.postCheckOldPass();
        }else{
            wx.showToast({
                icon:'none',
                title:'请输入当前密码'
            })
        }
    },
    newPassInput(e){
        console.log(e)
        this.setData({
            new_pass_word:e.detail.value
        });
    },
    oldPassInput(e){
        this.setData({
            old_pass_word:e.detail.value
        });
    },
    confirm(e) {
        if (this.data.showId) {
            let havePhone = Common.checkPhone(this.data.phone);
            let haveVerifyCode = Common.checkVerifyCode(this.data.verifyCode);
            console.log(this.data.phone + this.data.verifyCode);
            if (havePhone) {
                if (haveVerifyCode) {
                    this.postPhone();
                }
            }; 
        }else {
            this.posyCheckNewPass();
        }

    },
    postCheckOldPass(){
        HTTP.postData('/api/person/passOld',{
            password:this.data.old_pass_word
        },res=>{
            this.setData({
                next: !this.data.next
            })
        });
    },
    posyCheckNewPass(){
        HTTP.postData('/api/person/passNew',{
            password:this.data.new_pass_word
        },res=>{
            wx.showToast({
                icon:'none',
                title:'修改成功'
            });
            wx.navigateBack();
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.id === '1') {
            this.setData({
                showId: true
            })
        } else if (options.id === '2') {
            this.setData({
                showId: false
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})